﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;

namespace AllNations
{
    public partial class Login : MakeItWeb.Framework.MakeItWebPage
    {
        public Login()
        {
            UseQueryString = false;
        }

        protected override void OnInitComplete(EventArgs e)
        {
            Login1.LoggedIn += new EventHandler(Login1_LoggedIn);
            base.OnInitComplete(e);
            Page.Form.Action = "Login.aspx";
        }

        protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
        {
            e.Message.Body = @"Your account was set up on our Web site with the following:

    Username:   <%UserName%>

To confirm your email address, you must click on following link:

    http://www.anmc.us/MembershipConfirmation.aspx?ID=<%Confirmation%>
    
Thank you for creating an account with our Web site.";

            // Replace placeholder text in message body with information // provided by the user.
            var member = Member.GetMember(CreateUserWizard1.UserName);
            e.Message.Body = e.Message.Body.Replace("<%UserName%>", CreateUserWizard1.UserName);
            e.Message.Body = e.Message.Body.Replace("<%Confirmation%>", member.ConfirmationGuid.ToString());
        }

        void Login1_LoggedIn(object sender, EventArgs e)
        {
            string sRedirectUrl = Request.QueryString["Redirect"];
            if (string.IsNullOrEmpty(sRedirectUrl))
            {
                string sQuery = Request.UrlReferrer.Query;
                if (!string.IsNullOrEmpty(sQuery) && sQuery.Contains("Redirect="))
                    sRedirectUrl = sQuery.Substring(sQuery.IndexOf("=") + 1);
            }

            if (!string.IsNullOrEmpty(sRedirectUrl))
                Response.Redirect(sRedirectUrl, true);
            else
                Response.Redirect("~/MyProfile", true);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser);
            CreateUserWizard1.ContinueButtonClick += new EventHandler(CreateUserWizard1_ContinueButtonClick);
        }

        void CreateUserWizard1_ContinueButtonClick(object sender, EventArgs e)
        {
            Response.Redirect("~/MyProfile", true);
        }

        void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            System.Web.Security.Roles.AddUserToRole(CreateUserWizard1.UserName, "AuthenticatedUser");
        }

        protected void radMenuLeftNav_ItemClick(object sender, Telerik.Web.UI.RadMenuEventArgs e)
        {
            ANMCPageViews.SelectedIndex = e.Item.Index;
        }

        protected void PasswordAssistanceButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("/PasswordAssistant.aspx", true);
        }

        protected void RegistrationButton_Click(object sender, EventArgs e)
        {
            var menu = radMenuLeftNav.FindItemByValue("Registration");
            menu.Selected = true;
            ANMCPageViews.SelectedIndex = menu.Index;
        }
    }
}